/**
 * 中国国土资源部辽宁部”
 *  
 */
import { Base, Item } from '../base';
class zggtzylnxx extends Base {
  constructor() {
    super();
    this.name = '辽宁土地成交公示信息';
    this.enabled = true;
    this.encode = "utf-8";
    this.debug = false;
    this.timeout = 1000000;
    this.collection = "lnxm";
    this.collectionShowName = "辽宁项目爬虫";
    this.keyField = "title";
  }
  getItems(pageIndex:number) {
    let num = pageIndex + 1;
   let  url = `http://search.mlr.gov.cn/was/search?page=${num}&channelid=185667&searchword=xzq_dm%3D%E8%BE%BD%E5%AE%81&prepage=20`;
   (<any>this.spider).headers={};
   let content = this.spider.getSync(url);
    let $ = this.cheerio.load(content);
    let result:Item[] = [];
    let that = this;
    let first = $('.bord td table').first();
    try {
        $('.bord td table').not(first).each(function (index, ele) {
            let item = that.buildItem();
            let title = $(ele).find('tr').eq(0).find('td').eq(2).find('a').text().trim();
            let uri = $(ele).find('tr').eq(0).find('td').eq(2).find('a').attr('href');
            result.push(that.getExtraItem(uri, title));
        });
    }
    catch (error) {
        console.error(error);
    }
    return result;
}
getExtraItem(url:string, title:string) {
    (<any>this.spider).headers={};
    let content = this.spider.getSync(url);
    let $ = this.cheerio.load(content);
    let item = this.buildItem();
  try {
    item.title = title;
    (<any>item).url = url;
    (<any>item)['公示标题'] = $('.zw_title').text().trim();
    $('#content1>table').find('table').eq(0).find('tr').each(function (index, ele) {
        if($(ele).find('td').length==6){
         let arr=[0,2,4];
        arr.forEach(function(ele1,index1){
        let n=ele1+1;
        (<any>item)[$(ele).find('td').eq(ele1).text().trim()] = $(ele).find('td').eq(n).text().trim();
        });
        }else{
          (<any>item)[$(ele).find('td').eq(0).text().trim()] = $(ele).find('td').eq(1).text().trim();
        }
    });
   
  } catch (error) {
      
  }
  return item;
}
  getPageCount(): number {
    return 316;
  }
}
module.exports = new zggtzylnxx();